package com.mycompany.app;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

/*
* Hello world!
*/

public class App {
  public static void main(String[] args) throws Exception {
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        e.printStackTrace(); 
    }
    HttpServer server = HttpServer.create(new InetSocketAddress(18988), 0);
    server.createContext("/test", new MyHandler());
    server.setExecutor(null);
    System.out.println("============== Server startup, listen on port 18988... ==============");
    server.start();
  }

  static class MyHandler implements HttpHandler {
    public void handle(HttpExchange t) throws IOException {
      String response = "=== Welcome To SimpleHttpServer~ ===";
      t.sendResponseHeaders(200, response.length());
      OutputStream os = t.getResponseBody();
      os.write(response.getBytes());
      os.close();
    }
  }
}
